﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using ATSBLL;
using ATSModel;

public partial class ViewbyTProduct_MOBuilding : System.Web.UI.Page
{
    TMO tmo;
    THouse thouse;
    TProductType tp;
    TMOInfo ti;
    private string MOID { get { return ViewState["MOID"].ToString(); } set { ViewState["MOID"] = value; } }

    protected void Page_Load(object sender, EventArgs e)
    {
        this.Button3.Attributes.Add("onclick", "javascript:return confirm('删除此MO的同时，会删除下面的所有序列号，你确认要删除?')");
    }

    public ViewbyTProduct_MOBuilding()
    {
        tmo = new TMO();
        tp = new TProductType();
        thouse = new THouse();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string str;
        string moID = this.TextBox1.Text.Trim(); 

        string productTypeID = this.TextBox2.Text.Trim();
        string prefixSN = this.TextBox3.Text.Trim();
        try
        {
            int startSN = int.Parse(this.TextBox4.Text.Trim());
            int stopSN = int.Parse(this.TextBox5.Text.Trim());
            int SnCount = tmo.Exis(moID, prefixSN, startSN, stopSN).Count;
            if (!string.IsNullOrEmpty(tmo.LoadEntity(moID).MOID))
            {
                str = "此批次MO号已存在，请确认";
                this.TextBox1.Focus();

            }
            else if (SnCount > 0)
                {
                    str = "起始或终止编号范围已存在，请确认";
                    this.TextBox3.Focus();
                } 
            else if (startSN > stopSN)
            {
                str = "起始编号不能大于终止编号，请确认";
            }
            else if (string.IsNullOrEmpty(tp.LoadEntity(productTypeID).ProductTypeID))
            {
                str = "此产品类型不存在或没有录入过，请确认";
            }
            else
            {
                ti = new TMOInfo(moID, productTypeID, prefixSN, startSN, stopSN);
                if (tmo.Insert(ti))
                {
                    str = "录入成功";
                    this.TextBox1.Text = "";
                }
                else
                {
                    str = "录入失败";
                }
            }
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
        }
        catch
        {
            str = "起始或终止编号不是整数，请确认";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        MOID = this.TextBox6.Text.Trim();
        BindData();
    }

    protected void BindData()
    {
        this.GridView1.DataSource = thouse.GetTHousesbyMO(MOID);
        this.GridView1.DataBind();
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        string moID = this.TextBox7.Text.Trim();
        string str;
        if (tmo.Delete(moID))
        {
            this.TextBox7.Text = "";
            str = "删除成功！";
        }
        else
        {
            str = "删除失败，请联系相关人员！";
        }
        ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
    }

    protected void GridView1_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        BindData();
    }

    protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((LinkButton)e.Row.Cells[2].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除!')");
            }
        }
    }

    protected void GridView1_RowDeleting(object sender, System.Web.UI.WebControls.GridViewDeleteEventArgs e)
    {
        string productSN = GridView1.DataKeys[e.RowIndex].Values[1].ToString();
        thouse.Delete(productSN);
        BindData();
    }
}
